from selenium.webdriver.common.by import By
from base.basicFunction import *


class BusinessChangePage:
    def __init__(self, data):
        self.data = data

    def get_init_list(self):
        data = self.data
        t_unsubscribe_ls = []
        elements = wd.find_elements(By.XPATH, '//*[@id="UserOfferList_Table"]/div[1]/div/table/tbody/tr')
        for i in range(len(elements)):
            value = Element((By.XPATH, '//*[@id="UserOfferList_Table"]/div[1]/div/table/tbody/tr[%d]/td[2]' % (i + 1))).text()
            if value in data.keys(): t_unsubscribe_ls.append(data[value])

        logger.info(t_unsubscribe_ls)
        return t_unsubscribe_ls

    def business_order(self):
        logger.info("-" * 30 + "业务订购" + "-" * 30)
        data = self.data
        wait_for_load((By.ID, '191000000004'))  # 点击业务订购
        Element((By.ID, 'OfferSelectList_queryParam')).send_keys(data['策划编码'])  # 输入业务编码
        sleep(1)
        Element((By.XPATH, '//*[@id="OfferSelectList_queryCond"]/div[1]/div[2]/div/span/span')).click()
        Element((By.XPATH, '//*[@id="OfferSelectList_OfferUl"]/li/div[2]/button')).click()  # 点击订购
        # is_display((By.XPATH, '//*[starts-with(text(),"未选择任何策划")]/parent::div/parent::div/div[2]/button'))  # 判断策划是否选中
        sleep(2)
        if is_display((By.XPATH, '//*[starts-with(text(), "用户已经订购")]/../..//button'), False):
            raise Element((By.XPATH, '//*[starts-with(text(), "用户已经订购")]')).text()
        wait_for_load((By.XPATH, '//*[@id="ProdItem"]/div[7]/div/button'))  # 点击确定
        sleep(1)
        wait_for_load((By.XPATH, '//*[@id="CSSUBMIT_BUTTON"]/span'))
        logger.info("-" * 28 + "业务订购完成" + "-" * 28)
        sleep(1)
